﻿import Image
import sys
import os

def processImage(infile):
    try:
        im = Image.open(infile)
    except IOError:
        print "Cant load", infile
        sys.exit(1)
    im.save(infile.split('.')[0]+"."+infile.split('.')[1]+".bmp","BMP")
    return

    i = 0
    size        = im.size
    lastframe   = im.convert('RGBA')
    mypalette   = im.getpalette()

    try:
        while 1:
            im2 = im.copy()
            im2.putpalette( mypalette )

            background = Image.new("RGB", size, (255,255,255))

            background.paste( lastframe )
            background.paste( im2 )
            background.save('foo'+str(i)+'.png', 'PNG', quality=80)

            lastframe = background

            i += 1
            im.seek( im.tell() + 1 )
    except EOFError:
        pass # end of sequence

def process():
    path = "F:\\mywork\\oscQQ\\OpenCVPython\\yalefaces\\gif\\"
    for i in os.listdir(path):
        if os.path.isfile(os.path.join(path, i)):
            if i.split('.')[2] == 'gif':
                processImage(path+i)

def read_images(path, sz=None):
    c = 0
    X,y = [], []
    fp = open(os.path.join(path,"test.txt"),'w')
    for dirname, dirnames, filenames in os.walk(path):
        #print dirname
        #print dirnames
        #print filenames
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                str = "%s;%d\n"%(os.path.join(subject_path, filename), c)
                print str
                fp.write(str)
            c += 1
    fp.close()

if __name__ == '__main__':
    read_images("F:\\mywork\\oscQQ\\OpenCVPython\\OpencvSamples\\facerec_demo\\att_faces")
